/*
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 1, or (at your option)
** any later version.

** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.

** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,  USA.
*/

/*
Date: 18.09.2002
*/

/*
* Author : Boris Mikhailenko <stg34@ua.fm>
*/
//---------------------------------------------------------------------------
#ifndef ProtoUnitH
#define ProtoUnitH

#ifndef WIN32
    #include <netinet/in.h>
    #include <pthread.h>
#else
    #include <winsock2.h>
#endif

#include "common.h"
#include "packet.h"

#include <QThread>
#include <QString>

typedef void (*tpCallBackFn)(int);
typedef void (*tpCallBackInfoFn)(const char * message, int infoType, int showTime, int sendTime);
typedef void (*tpCallBackDirNameFn)(const string16 * dn, int);

typedef char string20[20];
//---------------------------------------------------------------------------
class CLIENTPROT : public QThread
{
    Q_OBJECT

public:
    //
    void run();
    //
    CLIENTPROT(unsigned long i, unsigned short p);
    ~CLIENTPROT();
    void Run();
    void Stop();
    void GetStat(LOADSTAT & ls);
    //void GetStat();
    void RefreshSettings();
    void SetServer(unsigned long ip, unsigned short port);
    void SetLogin(char *);
    void SetPassword(char *);
    //void SetCallBackFn(tpCallBackFn p);
    //void SetCallBackInfoFn(tpCallBackInfoFn p);
    //void SetCallBackDirNameFn(tpCallBackDirNameFn p);
    int Connect();
    int Disconnect();
    int GetStatus(); // Мы подключены или нет?
    int GetReconnect();
    void SetReconnect(int r);
    char GetProtoVer();
    void GetMessageText(char *, int);
    void GetInfoText(char *, int);
    int GetErrMes();
    bool GetNonstop() { return nonstop; };
private:
    //PROTTHREAD * thr;
    LOADSTAT stat;
    int NetRecv();
    int NetSend(int n);
    int * action;
    int phase;
    int phaseTime;                  // Время входа в фазу
    int PhaseValidate(int p);
    char messageText[236];
    char infoText[235];
    int errMes; //TODO Убрать нах!!!!
    bool nonstop;

    char password[PASSWD_LEN];
    char login[LOGIN_LEN];
    #ifdef WIN32
    WSADATA wsaData;
    #else
    pthread_t thread;
    #endif
    char srvIP[20];                     // ИП сервера
    int port;                           // Порт сервера
    int onePort;                        //
    int ip;                             // IP сервера
    struct sockaddr_in localAddrS;      // Наш адрес
    struct sockaddr_in localAddrR;      // Наш адрес
    struct sockaddr_in servAddr;        // адрес сервера
    int reconnect;
    //int socks;
    int sockr;
    int protNum;                    // Число, которое участвует в обмене сообщениями
    int userTimeout;
    int aliveTimeout;
    int buttonAct;
    unsigned int rd;
    //tpCallBackFn pCallBackFn;
    //tpCallBackInfoFn pCallBackInfoFn;
    //tpCallBackDirNameFn pCallBackDirNameFn;

    CONN_SYN            connSyn;
    CONN_SYN_ACK        connSynAck;
    CONN_ACK            connAck;
    ALIVE_SYN           aliveSyn;
    ALIVE_ACK           aliveAck;
    DISCONN_SYN         disconnSyn;
    DISCONN_SYN_ACK     disconnSynAck;
    DISCONN_ACK         disconnAck;
    ERR                 err;
    INFO                info;
    INFO_7              info7;

signals:
    void callBackFn(int a);
    void callBackInfoFn(const char * message, int infoType, int showTime, int sendTime);
    void callBackDirNameFn(const string16 * dn, int);

};
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#endif
